import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login.vue";
import About from "../views/About.vue";
import Cookie from "js-cookie";
import Refresh from "../components/Refresh.vue";

Vue.use(VueRouter);
// 解决点击重复路由出现报错部分开始
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
// 解决点击重复路由出现报错部分结束

const routes = [{
    path: "/",
    name: "Login",
    component: Login
  },
  {
    path: "/About",
    component: About,
    children: [{
      path: "/FirstPage",
      component: () => import("../components/FirstPage.vue"),
      meta: {
        title: '首页'
      }
    }, {
      path: '/Refresh',
      component: Refresh
    }, ]
  },
  {
    path: "/",
    name: "运营管理",
    component: About,
    children: [{
        path: "/OrderManagement",
        name: "订单管理",
        component: () => import("../components/OrderManagement.vue"),
        meta: {
          title: '订单管理'
        }
      },
      {
        path: "/comments",
        name: "评论管理",
        component: () => import("../components/comments.vue"),
        meta: {
          title: '评论管理'
        }
      },
      {
        path: "/FreightTemplate",
        name: "运费模板管理",
        component: () => import("../components/FreightTemplate.vue"),
        meta: {
          title: '运费模板管理'
        }
      }
    ]
  }, {
    path: "/",
    name: "商品管理",
    component: About,
    children: [{
      path: "/Goods",
      name: "商品列表",
      component: () => import("../components/Goods.vue"),
      meta: {
        title: '商品列表'
      }
    }, {
      path: "/category",
      name: "商品类目",
      component: () => import("../components/category.vue"),
      meta: {
        title: '商品类目'
      }
    }]
  }, {
    path: "/",
    name: "推广管理",
    component: About,
    children: [{
      path: "/BusinessAdvertising",
      name: "商铺广告",
      component: () => import("../components/BusinessAdvertising.vue"),
      meta: {
        title: '商铺广告'
      }
    }, {
      path: "/RecommendedManagement",
      name: "推荐管理",
      component: () => import("../components/RecommendedManagement.vue"),
      meta: {
        title: '推荐管理'
      }
    }, {
      path: "/CouponManagement",
      name: "优惠券管理",
      component: () => import("../components/CouponManagement.vue"),
      meta: {
        title: '优惠券管理'
      }
    }, {
      path: "/StoreInformation",
      name: "商铺信息",
      component: () => import("../components/StoreInformation.vue"),
      meta: {
        title: '商铺信息'
      }
    }]
  }, {
    path: "/",
    name: "系统管理",
    component: About,
    children: [{
      path: "/MemberManagement",
      name: "会员管理",
      component: () => import("../components/MemberManagement.vue"),
      meta: {
        title: "会员管理"
      }
    }, {
      path: "/Controllor",
      name: "管理员",
      component: () => import("../components/Controllor.vue"),
      meta: {
        title: "管理员"
      }
    }, {
      path: "/RoleManagement",
      name: "角色管理",
      component: () => import("../components/RoleManagement.vue"),
      meta: {
        title: "角色管理"
      }
    }]
  }
];

const router = new VueRouter({
  routes
});
// 路由守卫
router.beforeEach((to, from, next) => {
  // var routeName = to.name;
  //登录页面不用鉴权
  if (to.path === '/') {
    next();
  } else {
    var token = Cookie.get("token");//获取token
    if (token) {
      next();//有令牌可以进入
    } else {
      var returnUrl = to.fullPath;
      //next('/?returnUrl=' + '/Main');//返回登录授权，并且记录需要返回的路由
      next('/?returnUrl=' + returnUrl);//返回登录授权，并且记录需要返回的路由
    }
  }
})


export default router;